package org.jeecg.modules.common.service.impl;

import com.alibaba.fastjson.JSON;
import org.jeecg.modules.common.bean.mqtt.*;
import org.jeecg.modules.common.service.MqttGateway;
import org.jeecg.modules.common.service.MqttService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class MqttServiceImpl implements MqttService {

    @Autowired
    private MqttPahoMessageDrivenChannelAdapter adapter;

    @Autowired
    private MqttGateway mqttGateway;



    @Override
    public void addSubscribeTopic(String subscribeTopic) {
        String[] topics = adapter.getTopic();
        if(!Arrays.asList(topics).contains(subscribeTopic)){
            adapter.addTopic(subscribeTopic);
        }
    }

    @Override
    public void removeSubscribeTopic(String subscribeTopic) {
        adapter.removeTopic(subscribeTopic);
    }

    @Override
    public void sendMessage(String topic, String payload, Integer qos) {
        mqttGateway.sendToMqtt(topic,qos,payload);
    }

    @Override
    public void reboot(String topic, Integer qos) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setCmd("rst");
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void setCyc(String topic, Integer qos, MqttSysBean mqttSettingBean) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setSys(mqttSettingBean);
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void setThv(String topic, Integer qos, MqttThresholdBean mqttThresholdBean) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setThv(mqttThresholdBean);
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void setAI(String topic, Integer qos, MqttAIBean mqttAIBean) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setAi(mqttAIBean);
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void setDI(String topic, Integer qos, MqttDiBean mqttDiBean) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setDi(mqttDiBean);
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void setCMD(String topic, Integer qos, String mqttCmdBean) {
        MqttSettingBean mqttControlBean = new MqttSettingBean();
        mqttControlBean.setCmd("getpara");
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }

    @Override
    public void control(String topic, Integer qos,MqttControlBean mqttControlBean) {
        mqttGateway.sendToMqtt(topic, JSON.toJSONString(mqttControlBean));
    }


}
